App.controller('publishMakeBillController', ['$rootScope', '$scope', '$state', 'Page', function ($rootScope, $scope, $state, Page) {
    $scope.makeBillGrid = {

        column_defs: [
            {
                target : 1,
                template: function(row) {
                    return '<a href="javascript:void(0)" ed-click="view">' + row.billNo + '</a>';
                }
            },
            {
                target: 6,
                buttons: [
                    {
                        name: '明细',
                        class: 'btn-edit',
                        icon: 'fa-edit',
                        event: 'details'
                    }
                ]
            }
        ],
        details : function(row) {
            $scope.makeBillDetailGrid.setDynamicTable('publishBill/busOrStopHead.do',
                {billType : row.billType, makeBillId : row.makeBillId});
            $scope.makeBillModal.show();
        },
        view : function(row) {
            $scope.viewBillNo = row.billNo;
            $scope.viewBillType = row.billType;
            Page.ajaxPostQuiet('publishBill/queryBillDetails.do', {billId : row.billId}, function (resp) {
                var data = resp.data.array;
                $scope.viewResources = new Map();
                $scope.viewMediaTypeMap = new Map();
                if(data != undefined && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        if($scope.viewMediaTypeMap.get(data[i].mediaType) == undefined) {
                            $scope.viewMediaTypeMap.put(data[i].mediaType,data[i].mediaName);
                        }
                        $scope.viewResources.put(data[i].resourceId,data[i]);
                    }
                }
                $scope.billViewModal.show();
            });
        }
    }
    $scope.makeBillDetailGrid = {
        options : {
            bAutoWidth:false
        },
        column_defs: [
            {
                target: 7,
                buttons: [
                    {
                        name: '完工',
                        class: 'btn-edit',
                        icon: 'fa-edit',
                        event: 'complete',
                        visiable : function(row) {
                            if(row.makeBillDetailStatus == "1") {
                                return true;
                            }
                            return false;
                        }
                    },
                    {
                        name: '查看',
                        class: 'btn-edit',
                        icon: 'fa-edit',
                        event: 'view',
                        visiable : function(row) {
                            if(row.makeBillDetailStatus == "2") {
                                return true;
                            }
                            return false;
                        }
                    }
                ]
            }
        ],
        complete : function(row) {
            $scope.completeBill = row;
            $scope.completeModal.show();
        },
        view : function(row) {
            $scope.completeBill = row;
            $scope.completeViewModal.show();
        }
    }

    $scope.completeModal = {
        onOkClick : function() {
            Page.ajaxPostQuiet('publishBill/completeBill.do', $scope.completeBill, function (resp) {
            	
            	// 获得制作单明细的表头
                $scope.makeBillDetailGrid.setDynamicTable('publishBill/busOrStopHead.do',
                    {billType : $scope.completeBill.billType, makeBillId : $scope.completeBill.makeBillId});
                $scope.completeModal.hide();
            });
        },
        onHidden : function() {

        }
    }

    $scope.fileModal = {
    		deleteFile : function(file){
    			var files =$scope.completeBill.img;
    			 for (var i = 0; i < files.length; i++) {
    				 if(files[i].path.indexOf(file.path)>-1){
    						 $scope.completeBill.img.splice(i,1);
    				 }
    			 }
    		},
		  downloadFile : function(filePath) {
	        	document.location.href = "publishBill/downLoadFile.do?publishFile="+filePath;
	        },
    }
}]);
